<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图片切换</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    img {
      display: block;
    }
  </style>
</head>

<body>
  <img id="img" src="./img/1.jpg" alt="图片" width="200" height="200">
  <button id="prev">上一张</button>
  <button id="next">下一张</button>
</body>
<script>
  "use strict"; {
    let img = document.getElementById("img");
    let prev = document.getElementById("prev");
    let next = document.getElementById("next");
    let min_index = 1, max_index = 4, current_index = 1;
    next.addEventListener("click", () => {
      if (current_index === max_index) {
        current_index = 1;
      } else {
        current_index++;
      };
      img.setAttribute("src", `./img/${current_index}.jpg`);
    });
    prev.addEventListener("click", () => {
      if (current_index === min_index) {
        current_index = 4;
      } else {
        current_index--;
      };
      img.setAttribute("src", `./img/${current_index}.jpg`);
    });
  };
</script>

</html>